
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include "gamedrawer.h"
using namespace std;

// Window size in pixels
int WindowHeight;
int WindowWidth;

//The number of milliseconds between calls to update
const int TIMER_MS = 25;

GameDrawer* gameDrawer;

void cleanup() {
	delete gameDrawer;
}

void handleKeypress(unsigned char key, int x, int y) {

	switch (key) {	
			break;
		case 27: //Escape key
			//cleanup();
			exit(0);
		default:
			gameDrawer->handleKeyboardInput(key, x, WindowHeight-y);

	}
}

void handleSpecialKeypress(int key, int x, int y) {

}

void handleSpecialKeyReleased(int key, int x, int y) {

}

void handleMouse(int button, int state, int x, int y ){
		gameDrawer->handleMouseInput(button, state, x, WindowHeight-y);
}
void motion(int x, int y ){
		
		float xPos = ((float)x)/((float)(WindowWidth-1));
		float yPos = ((float)y)/((float)(WindowHeight-1));

		yPos = 1.0f-yPos;// Flip value since y position is from top row.
		gameDrawer->moveMouse(x, WindowHeight-y);
		gameDrawer->moveActive(x,WindowHeight-y);
}
void passiveMotion(int x, int y ){
		
		float xPos = ((float)x)/((float)(WindowWidth-1));
		float yPos = ((float)y)/((float)(WindowHeight-1));

		yPos = 1.0f-yPos;// Flip value since y position is from top row.
		gameDrawer->moveMouse(x, WindowHeight-y);
}

void initRendering() {
	//glEnable(GL_DEPTH_TEST);
	glEnable(GL_COLOR_MATERIAL);
	//glEnable(GL_LIGHTING);
	glEnable(GL_NORMALIZE);
	//glEnable(GL_CULL_FACE);
	glShadeModel(GL_SMOOTH);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


//	initGameDrawer();
}

void handleResize(int w, int h) {
	WindowHeight = (h>1) ? h : 2;
	WindowWidth = (w>1) ? w : 2;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (float)w / (float)h, 0.02, 5.0);
	gameDrawer->updateSize(WindowWidth, WindowHeight);
}

float currentScale = 1;
float zoomTranslateX = 0;
float zoomTranslateY = 0;
float totalMovedX = 0;
float totalMovedY = 0;

void zoom(float deltaZoom){
  currentScale = currentScale + deltaZoom;
  float xAdjust = (WindowWidth*deltaZoom/2);
  float yAdjust = (WindowHeight*deltaZoom/2);
  zoomTranslateX -= xAdjust;
  zoomTranslateY -= yAdjust;
}


void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
	glTranslated(-zoomTranslateX, -zoomTranslateY, 0);

	glTranslated(totalMovedX, totalMovedY, 0);
	glScaled(currentScale, currentScale, 1);

	gameDrawer->draw();	
	glutSwapBuffers();

	glPopMatrix();
}

void update(void) {
	gameDrawer->advance((float)TIMER_MS / 1000);	
	glutPostRedisplay();
}

int main(int argc, char** argv) {
	WindowWidth = 1024;
	WindowHeight = 768;
	//srand((unsigned int)time(0)); //Seed the random number generator
	srand(8587389); //Seed the random number generator	
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(WindowWidth, WindowHeight);
	glutCreateWindow("Links");
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);
	glEnable(GL_LINE_SMOOTH);
	initRendering();
	
	gameDrawer = new GameDrawer(WindowWidth, WindowHeight);
	
	glutDisplayFunc(drawScene);
	glutKeyboardFunc(handleKeypress);
	glutMouseFunc(handleMouse);
	glutMotionFunc(motion);
	glutPassiveMotionFunc(passiveMotion);
	glutSpecialFunc(handleSpecialKeypress);
	glutSpecialUpFunc(handleSpecialKeyReleased);
	glutReshapeFunc(handleResize);
	glutIdleFunc(update);
	glutMainLoop();
	return 0;
}









